Page({
  data: {
    houseList: [],
    isEmpty: false,
    dialogVisible: false,
  },
  onShow(){
    // 获取用房屋列表
    this.getHouseList()
  },
    // 用户房屋列表
    async getHouseList() {
      // 调用接口
      const houseList = await wx.http.get('/room')
      // 渲染数据
      this.setData({
        houseList,
        isEmpty: houseList.length === 0,
      })
    },

  async swipeClose(ev) {
    const { position, instance } = ev.detail
    if (position === 'right') {
      // 显示 Dialog 对话框
      // this.setData({
      //   dialogVisible: true,
      // })

      const {confirm} = await wx.showModal({
        content: '是否删除已绑定房屋？',
        confirmText:'删除'
      })
      //点击确定的按钮
      if(confirm){
        this.deleteHouse(ev.mark.index,ev.mark.id)
      }

      // swiper-cell 滑块关闭
      instance.close()
    }
  },

   /**
    * 删除房屋
    * @param {*} index 点击的索引
    * @param {*} id 点击的id
    */
   async deleteHouse(index,id) {
    // 调用接口
    await wx.http.delete('/room/' + id)
    // 从 AppData 中将删除的房屋从数组移除
    this.data.houseList.splice(index, 1)
    // 渲染数据
    this.setData({
      houseList: this.data.houseList,
      isEmpty: this.data.houseList.length === 0,
    })
  },

  goDetail(ev) {
    wx.navigateTo({
      url: '/house_pkg/pages/detail/index?id='+ev.mark.id,
    })
  },

  addHouse() {
    wx.navigateTo({
      url: '/house_pkg/pages/locate/index',
    })
  },
})
